package com.dong.rabbitmq.controller;

import com.alibaba.fastjson.JSON;
import com.dong.rabbitmq.config.DelayedQueueConfig;
import com.dong.rabbitmq.config.OcrMqConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.plugin2.message.Message;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author dongjian
 * 发送消息控制层
 * <p>
 * ttl队列
 */
@RestController
@RequestMapping("/ttl")
@Slf4j
public class SendMsgController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     * 发送延迟消息，队列提前定义好消息的TTL时间
     *
     * @param message 消息内容
     */
    @GetMapping("/sendMsg_ttl/{message}")
    public void sendMsgTtl(@PathVariable String message) {
        log.info("当前时间：{}, 发送一个消息给两个TTL队列：{}", LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME), message);
        rabbitTemplate.convertAndSend("X", "XA", "发送一条来自ttl为10s的队列消息：" + message);
        rabbitTemplate.convertAndSend("X", "XB", "发送一条来自ttl为40s的队列消息：" + message);
    }

    /**
     * 在死信队列的基础上发送延迟消息
     * 缺点：队列只能检测第一个消息的TTL
     *
     * @param message 消息内容
     * @param ttlTime TTL时间 单位ms
     */
    @GetMapping("/sendMsg_ttl/{message}/{ttlTime}")
    public void sendMsgTtlTime(@PathVariable String message, @PathVariable String ttlTime) {
        log.info("当前时间：{}, 发送一个消息给延迟时长 {}ms的消息给TTL队列：{}", LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME), ttlTime, message);
        rabbitTemplate.convertAndSend("X", "XC", message, msg -> {
            msg.getMessageProperties().setExpiration(ttlTime);
            return msg;
        });
    }

    /**
     * 利用rabbitmq_delayed_message_exchange插件实现延迟队列消息发送
     *
     * @param message     消息内容
     * @param delayedTime 延迟时间 单位ms
     */
    @GetMapping("/sendMsg_delay/{message}/{delayedTime}")
    public void sendMsgDelayedQueue(@PathVariable String message, @PathVariable Integer delayedTime) {
        log.info("当前时间：{}, 发送一个消息给延迟时长 {}ms的消息给delayed.queue队列：{}", LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME), delayedTime, message);
        rabbitTemplate.convertAndSend(DelayedQueueConfig.DELAYED_EXCHANGE_NAME, DelayedQueueConfig.DELAYED_ROUTING_KEY, message, msg -> {
            msg.getMessageProperties().setDelay(delayedTime);
            return msg;
        });
    }

    @GetMapping("/sendMsg_ocr/{message}")
    public void sendMsgOcr(@PathVariable String message) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i <= 20; i++) {
            String msg = message + "_" + i;
            list.add(msg);
        }
        rabbitTemplate.convertAndSend(OcrMqConfig.EXCHANGE_NAME, OcrMqConfig.ROUTING_KEY_FILEINFO, String.valueOf(JSON.toJSON(list)));
    }
}
